// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin 1746365080@qq.com
 * @Date: 2024-10-24 14:51:52
 * @LastEditors: qiulin_JXGM ql1746365080@gmail.com
 * @LastEditTime: 2024-12-06 19:58:31
 * @FilePath: /SGSim/DBManager/UnitTest/HDF5/Input/NodeTest.cpp
 */

#include <algorithm>
#include <exception>
#include <iostream>
#include <vector>

#include <gtest/gtest.h>

#include "DataStructure/Common/Id.h"
#include "DataStructure/Input/Node/NodeData.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Node/INodeService.h"
#include "DBManager/IService/Input/Node/ISPOINTService.h"
#include "DBManager/IService/Input/Node/IPointService.h"
#include "DBManager/UnitTest/HDF5/TestDB.h"

using namespace SG::DBManager;
using namespace SG::DataStructure::FEM;
class I_NodeTest : public testing::Test
{
  public:
    static void SetUpTestSuite ()
    {
        if (DB::ServiceFactory != nullptr)
        {
            DB::ServiceFactory->ClearDatabase ();
        }
    }
};

TEST_F (I_NodeTest, Node)
{
    std::vector<SG::DataStructure::Common::Id> ids = { 45, 17, 92, 3, 78, 54, 29, 63, 10, 87, 36, 50, 19, 71, 8, 96, 12, 68, 25, 41 };

    SG::DataStructure::FEM::NodeData node;

    try
    {
        auto ser = DB::ServiceFactory->get<INodeService> ();
        for (auto i = 0; i < 1 * 10; i++)
        {
            node.m_id = ids[i];
            ser->Add (node);
        }

        std::vector<SG::DataStructure::Common::Id> const a2 = ser->GetAllId ();

        for (auto&& i : a2)
        {
            ASSERT_TRUE (std::find (ids.begin (), ids.end (), i) != ids.end ());
        }

        ASSERT_TRUE (ser->Size ());
        ASSERT_TRUE (ser->FindByIndex (5, node) == SG::DataStructure::Common::Status::STATUS_Success);
        ASSERT_EQ (node.m_id, 54);
        ASSERT_EQ (ser->FindById (92, node), SG::DataStructure::Common::Status::STATUS_Success);
        ASSERT_EQ (node.m_id, 92);
    }
    catch (const std::exception& e)
    {
        std::cerr << e.what () << '\n';
    }
}

TEST_F (I_NodeTest, SPOINTData)
{
    SG::DataStructure::Common::Id const id = 99;
    // 创建对象
    SPOINTData obj;
    obj.m_id = id;

    // 获取服务
    auto service = DB::ServiceFactory->get<ISPOINTService> ();
    // 添加对象
    service->Add (obj);

    ASSERT_EQ (service->Size (), 1);
}


TEST_F (I_NodeTest, PointData)
{
    SG::DataStructure::Common::Id const id = 100;
    // 创建对象
    PointData obj;
    obj.m_id = id;

    // 获取服务
    auto service = DB::ServiceFactory->get<IPointService> ();
    // 添加对象
    service->Add (obj);

    std::vector<SG::DataStructure::Common::Id> ids = service->GetAllId();

    ASSERT_EQ (ids.size (), 1);
    ASSERT_EQ (service->Size (), 1);
}